# -*- coding: utf-8 -*-

import time
from odoo import http
import logging
from odoo.http import request
# 相对导入当前目录下的abc模块
from .abc import abc

_logger = logging.getLogger(__name__)
from odoo import http
class NotifyController(http.Controller):
    @http.route('/esign/notify', auth='public', type='http', methods=['GET'], csrf=False)
    def action_esign(self, **kw):
        """
        返回一个公开页面，所有人都可以访问。
        """
        response_data = {
            "code": "200",
            "msg": "success"
        }
        return "33333"
 
class MyController(http.Controller):

    @http.route('/my_module/get_data', type='json', auth='user')
    def get_abc_data(self):
        # 访问导入模块中的变量
        result = abc.a1
        _logger.info("成功获取变量值: %s", result)
        return {
            'status': 'success',
            'data': result
        }
    @http.route('/my_module/websoket')  
    def websoket(self):  
        message_text = "12345678998765412345678987654321"  
        
        # 保存当前环境信息  
        dbname = request.env.cr.dbname  
        uid = request.env.uid  
        context = request.env.context  
        
        def send_typing_messages():  
            # 在线程中重新创建环境  
            from odoo.modules.registry import Registry  
            from odoo import api  
            
            db_registry = Registry(dbname)  
            with db_registry.cursor() as cr:  
                env = api.Environment(cr, uid, context)  
                
                for i, char in enumerate(message_text):  
                    partial_message = message_text[:i+1]  
                    env['bus.bus']._sendone(  
                        'channel001',  
                        'typing_message',  
                        {  
                            'text': char,  
                            'is_complete': i == len(message_text) - 1,  
                            'char_index': i  
                        }  
                    )  
                    # 提交事务确保消息立即发送  
                    cr.commit()  
                    time.sleep(0.1)  
        
        import threading  
        thread = threading.Thread(target=send_typing_messages)  
        thread.start()  
        
        return "Messages started"
       



    _return_url = '/payment/zhifubao/return'
    _webhook_url = '/payment/zhifubao/webhook'

    @http.route(
        _return_url, type='http', auth='public', csrf=False, save_session=False
    )
    def zhifubao_return_from_checkout(self, **data):

        _logger.info("支付宝回调数据_return_url：%s", data)
        # Check the integrity of the notification.
        tx_sudo = request.env['payment.transaction'].sudo()._get_tx_from_notification_data(
            'zhifubao', data
        )


        # Handle the notification data.
        tx_sudo._handle_notification_data('zhifubao', data)
        return request.redirect('/payment/status')

    @http.route(_webhook_url, type='http', auth='public',  csrf=False)
    def zhifubao_webhook(self, **data):
      
    
        try:
            # Check the integrity of the notification.
            tx_sudo = request.env['payment.transaction'].sudo()._get_tx_from_notification_data(
                'zhifubao', data
            )

            # Handle the notification data.
            tx_sudo._handle_notification_data('zhifubao', data)
        except :  # Acknowledge the notification to avoid getting spammed.
            _logger.exception("Unable to handle the notification data; skipping to acknowledge.")

        return ''  # Acknowledge the notification.
